extends FunctionCard

func _init() -> void:
	cost = 5
	card_id = "QingYiSe"

func _ready() -> void:
	card_name = "清一色"
	self_destruction = false
	self_description = "结算结束后，对打出的牌验证，若为清一色，则额外倍率+5"
	trigger_timing = 3
	label.text = card_name
	self_descrpition_label.text = self_description
	

func cardFunction(deck,scoreMapping,selectedCardsDetail):
	var parent = get_parent()  # 获取父节点
	var mahjongIdArr = []
	if parent:
		var grandParent = parent.get_parent()  # 获取父节点的父节点（即祖父节点）
		var selectedMahjongs = grandParent.selectedMahjongs
		for mahjong in selectedMahjongs:
			var mahjongId = mahjong.mahjong_id
			mahjongIdArr.append(mahjongId)
		if is_all_same_prefix(mahjongIdArr):
			run_detail.text = "清一色～"
			showRunDetail()
			await get_tree().create_timer(0.5).timeout  # 等待
			hideRunDetail()
			grandParent.playingMagnification += 5
			await grandParent.updatePlayingMagnificationLabel() # 更新出牌倍率
			if grandParent.playingMagnification > RoundDetail.magFire:
				grandParent.playFire()
		else:
			run_detail.text = "不是～"
			showRunDetail()
			await get_tree().create_timer(0.5).timeout  # 等待
			hideRunDetail()

func is_all_same_prefix(arr: Array) -> bool:
	if arr.size() == 0:
		return false
	# 检查是否全是 "wan"
	if check_prefix(arr, "wan"):
		return true
	# 检查是否全是 "tiao"
	if check_prefix(arr, "tiao"):
		return true
	# 检查是否全是 "tong"
	if check_prefix(arr, "tong"):
		return true
	
	# 如果都不是，返回 false
	return false

func check_prefix(arr: Array, prefix: String) -> bool:
	for item in arr:
		if not item.begins_with(prefix):
			return false
	return true
